package LinkedList.两数相加;

import LinkedList.ListNode;

public class Test {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        int n = 0; // 要进位的值，第一次可以当成 0
        while(l1 != null || l2 != null || n != 0) {
            // 当前如果为空 可以当成 0 来处理
            int val1 = l1 != null ? l1.val : 0;
            int val2 = l2 != null ? l2.val : 0;
            int sum = val1 + val2 + n;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;
            if(l1 != null) {
                l1 = l1.next;
            }
            if(l2 != null) {
                l2 = l2.next;
            }
            n = sum / 10;
        }
        return dummy.next;

    }
}
